package com.example.front.serivce;

import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.ButtonDto;
import com.example.front.dto.ButtonSearchDto;
import com.example.front.entity.Button;
import com.example.front.mapper.ButtonMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ButtonService {
    @Resource
    ButtonMapper buttonMapper;
    @Resource
    EventService eventService;
    @Resource
    CategoryService categoryService;

    /**
     * 添加按钮
     */
    @Transactional
    public void createButton(Button button) {
        //查category是否存在
        categoryService.findById(button.getTenantId(), button.getCategoryId());
        //key在同一盒子类型category中不能重复
        if (buttonMapper.findButtonByKey(button) != 0) {
            throw new BusinessException("该盒子类此key所占按钮已存在");
        }
        //查event是否存在
        eventService.findById(button.getTenantId(), button.getEventId());
        //event在同一盒子类型category中不能重复
        if (buttonMapper.findButtonByEvent(button) != 0) {
            throw new BusinessException("该盒子类此事件已存在");
        }

        if (buttonMapper.create(button) == 0) {
            throw new BusinessException("操作异常");
        }
    }

    /**
     * 删除
     */
    public void deleteButton(Button button) {
        if (buttonMapper.delete(button) == 0) {
            throw new BusinessException("按钮不存在或操作异常");
        }
    }

    public void deleteByCategoryId(Button button) {
        buttonMapper.deleteByCategoryId(button);
    }

    /**
     * 列出列表
     */
    public ListDto<ButtonDto> list(ButtonSearchDto condition, Pagination pagination) {
        long total = buttonMapper.count(condition);
        if (total <= 0) {
            return new ListDto<>(new ArrayList<>(), pagination);
        }
        pagination.setTotal(total);

        List<Button> buttonList = buttonMapper.findAll(condition, pagination);
        List<ButtonDto> buttonDtoList = buttonList.stream().map(box -> {
            ButtonDto buttonDto = new ButtonDto();
            BeanUtils.copyProperties(box, buttonDto);
            buttonDto.setEventName(eventService.findById(condition.getTenantId(), box.getEventId()));
            return buttonDto;
        }).collect(Collectors.toList());

        return new ListDto<>(buttonDtoList, pagination);
    }

    /**
     * 修改
     */
    @Transactional
    public void update(Button button) {
        if (buttonMapper.findById(button) == null) {
            throw new BusinessException("按钮不存在");
        }
        eventService.findById(button.getTenantId(), button.getEventId());
        if (buttonMapper.findButtonByEvent(button) != 0) {
            throw new BusinessException("该盒子类此事件已存在");
        }
        categoryService.findById(button.getTenantId(), button.getCategoryId());
        if (buttonMapper.findButtonByKey(button) != 0) {
            throw new BusinessException("该盒子类此key所占按钮已存在");
        }
        if (buttonMapper.update(button) != 1) {
            throw new BusinessException("操作异常");
        }

    }

    /**
     * 获取事件（按下按钮
     */
    public ButtonDto event(Button button) {

        ButtonDto buttonDto = buttonMapper.event(button);
        if (button == null) {
            throw new BusinessException("操作异常");
        }

        return buttonDto;
    }

    /**
     * 根据事件查找按钮
     */
    public long findByEventId(long tenantId, long id) {
        return buttonMapper.findByEventId(tenantId, id);
    }

    public Button findById(long tenantId, long id) {
        Button button =  Button.builder().id(id).tenantId(tenantId).build();
        return buttonMapper.findById(button);
    }

}
